package org.agora.photogallery.client.common;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;

public class XmlUtils {

    public static Node dig(NodeList nodeList , String ...strings ){
	Node node = null;
	for (String s : strings ){
	    node = findByName(nodeList, s);
	    nodeList = node.getChildNodes();

	}
	if(node == null){
	    System.out.println("no node found!!!");
	}
	return node;


    }

    public static Node findByName(NodeList nodeList, String name){
	for(int i =0; i<nodeList.getLength(); i++){
	    Node node = nodeList.item(i);
	    if(node.getNodeName().equals(name)){
		return node;
	    }
	}
	return null;
    }

    public static NodeList findAllByName(NodeList nodeList, String name){
	class MyNodeList implements NodeList {
	    private List<Node> l = new ArrayList();
	    public void add(Node item){
		l.add(item);
	    }
	    @Override
	    public int getLength() {
		// TODO Auto-generated method stub
		return l.size();
	    }

	    @Override
	    public Node item(int index) {
		// TODO Auto-generated method stub
		return l.get(index);
	    }

	}
	MyNodeList list = new MyNodeList();
	for(int i =0; i<nodeList.getLength(); i++){
	    Node node = nodeList.item(i);
	    if(node.getNodeName().equals(name)){
		list.add(node);
	    }
	}
	return (NodeList) list;
    }

}
